'use strict';

module.exports = app => {
  const { model } = app;
  const { UUID, STRING, ENUM, INTEGER } = app.Sequelize;

  const Menu = model.define(
    'Menu',
    {
      id: {
        type: UUID,
        primaryKey: true,
      },
      parent_id: {
        type: UUID,
      },
      name: {
        type: STRING(50),
        allowNull: false,
      },
      url: {
        type: STRING(255),
        allowNull: false,
      },
      icon: {
        type: STRING(255),
        allowNull: true,
      },
      description: {
        type: STRING(255),
        allowNull: true,
      },
      sort: {
        type: INTEGER,
        allowNull: true,
        defaultValue: 0,
      },
      type: {
        type: ENUM,
        values: [ '0', '1' ],
        defaultValue: '0',
      },
      create_by: {
        type: UUID,
        allowNull: false,
      },
      update_by: {
        type: UUID,
        allowNull: false,
      },
    },
    {
      tableName: 'sys_menu',
    }
  );

  Menu.associate = function() {
    const { Sys: { Permission, Menu, PermissionMenu } } = model;
    Menu.belongsToMany(Permission, { as: 'permissions', foreignKey: 'menu_id', through: PermissionMenu });
  };

  return Menu;
};
